/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package db;

import beans.B_Persona;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import beans.B_Usuario;
import conexion.Conexion;
import java.sql.Types;

/**
 *
 * @author JoseHernan
 */
public class DB_Usuario {
    //SP SELECT SOLO DE VISTAS O TABLAS

    public static ArrayList<B_Usuario> getList(int id) {
        ArrayList<B_Usuario> list = new ArrayList<B_Usuario>();
        try {
            Connection con = Conexion.getConexion();

            PreparedStatement ps;
            ResultSet rs;
            if (id == 0) {
                ps = con.prepareStatement(" SELECT u.id, u.usuario, u.id_persona, p.apellidos, p.nombres FROM tbl_usuario u inner join tbl_persona p on p.id = u.id_persona where u.removed = false ");
            } else {
                ps = con.prepareStatement(" SELECT u.id, u.usuario, u.id_persona, p.apellidos, p.nombres FROM tbl_usuario u inner join tbl_persona p on p.id = u.id_persona where u.removed = false and u.id = " + id);
            }
            rs = ps.executeQuery();
            B_Usuario obj;
            B_Persona objPersona;
            while (rs.next()) {
                obj = new B_Usuario();
                //Asign values
                obj.setId(rs.getInt("id"));
                obj.setUsuario(rs.getString("usuario"));
                objPersona = new B_Persona();
                objPersona.setId(rs.getInt("id_persona"));
                objPersona.setApellidos(rs.getString("apellidos"));
                objPersona.setNombres(rs.getString("nombres"));
                obj.setObjPersona(objPersona);
                //Add to List
                list.add(obj);
            }
            rs.close();
            ps.close();
            con.close();
        } catch (Exception ex) {
            System.out.println(ex);
        }
        return list;
    }

    //SP CRUD
    public static String crud(B_Usuario obj, String action) {
        String result = "";
        CallableStatement cs = null;
        Connection con = Conexion.getConexion();
        try {
            cs = con.prepareCall("{call sp_crud_usuario(?,?,?,?,?,?,?)}");

            cs.registerOutParameter(1, Types.VARCHAR);
            cs.setString(2, action);
            cs.setInt(3, obj.getId());
            cs.setString(4, obj.getUsuario());
            cs.setString(5, obj.getPassword());
            cs.setString(6, obj.getUser());
            cs.setInt(7, obj.getObjPersona().getId());
            cs.executeUpdate();
            result = cs.getString(1);
        } catch (Exception ex) {
            result = ex.getMessage();
        } finally {
            try {
                cs.close();
                con.close();
            } catch (Exception ex) {
                System.out.println(ex);
            }
        }
        return result;
    }

    //SP AUTHENTICATION
    public static ArrayList<String> authentication(B_Usuario obj) {
        ArrayList<String> result = new ArrayList<String>();
        CallableStatement cs = null;
        Connection con = Conexion.getConexion();
        try {
            cs = con.prepareCall("{call sp_authentication(?,?,?,?,?)}");

            cs.setString(1, obj.getUsuario());
            cs.setString(2, obj.getPassword());
            cs.registerOutParameter(3, Types.VARCHAR);
            cs.registerOutParameter(4, Types.VARCHAR);
            cs.registerOutParameter(5, Types.VARCHAR);

            cs.executeUpdate();
            String resultado = cs.getString(3);
            String userName = cs.getString(4);
            String idUsuario = cs.getString(5);
            result.add(resultado);
            result.add(userName);
            result.add(idUsuario);
//            result.add(0,cs.getString(1));
//            result.add(1,cs.getString(2));
        } catch (Exception ex) {
            result.add(0, ex.getMessage());
        } finally {
            try {
                cs.close();
                con.close();
            } catch (Exception ex) {
                System.out.println(ex);
            }
        }
        return result;
    }
}
